﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleApp10;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        OrderService orderService;
        BindingSource fieldsBS = new BindingSource();
        public String Keyword { get; set; }
        public Form1()
        {
            InitializeComponent();
            orderService = new OrderService();
            Order order = new Order("1", new Customer("1", "li"), new List<OrderItem>());
            order.AddItem(new OrderItem(1, new Goods("1", "apple", 100.0), 10));
            orderService.AddOrder(order);
            Order order2 = new Order("2", new Customer("2", "zhang"), new List<OrderItem>());
            order2.AddItem(new OrderItem(1, new Goods("2", "egg", 200.0), 10));
            orderService.AddOrder(order2);
            bindingSource1.DataSource = orderService.Orders;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = textBox1.Text;
            string name = textBox2.Text;
            Order order = new Order("3", new Customer(id, name), new List<OrderItem>());
            order.AddItem(new OrderItem(1, new Goods("1", "apple", 100.0), 10));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string id = textBox1.Text;
            orderService.RemoveOrder(id);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                bindingSource1.DataSource = orderService.QueryOrdersByCustomerName(Keyword);
            else
            {
                int.TryParse(Keyword, out int id);
                Order order = orderService.GetOrder(id);
                List<Order> result = new List<Order>();
                if (order != null) result.Add(order);
                bindingSource1.DataSource = result;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                String fileName = saveFileDialog1.FileName;
                orderService.Export(fileName);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                String fileName = openFileDialog1.FileName;
                orderService.Import(fileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Order order = bindingSource1.Current as Order;
            if (order == null)
            {
                MessageBox.Show("请选择一个订单进行修改");
                return;
            }
        }
    }
}
